package com.zrrd.yunchmall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.j2objc.annotations.AutoreleasePool;
import com.zrrd.yunchmall.product.entity.ProductAttribute;
import com.zrrd.yunchmall.product.entity.ProductCategory;
import com.zrrd.yunchmall.product.service.IProductAttributeCategoryService;
import com.zrrd.yunchmall.product.service.IProductAttributeService;
import com.zrrd.yunchmall.product.service.IProductCategoryService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.framework.qual.HasQualifierParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 商品属性参数Rest控制器
 * </p>
 *
 * @author YeKong
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/productAttribute")
@Api(tags = "商品服务-商品属性模块")
public class ProductAttributeController {
    @Autowired
    private IProductAttributeService attributeService;

    @GetMapping("/list/{attributeCateId}")
    @ApiOperation(value = "查询列表")
    public ResponseResult list(@PathVariable("attributeCateId") int cateId,
                               @RequestParam(required = false) Integer pageNum,
                               @RequestParam(required = false) Integer pageSize,
                               @RequestParam(required = false) Integer type) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_attribute_category_id", cateId);
        if (type != null) {
            queryWrapper.eq("type", type);
        }
        if (pageNum == null || pageSize == null) {
            return new ResponseResult(200, "查询成功", attributeService.page(new Page<>(1, 100), queryWrapper));
        }

        return new ResponseResult(200, "查询成功", attributeService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @ApiOperation("删除属性表")
    @PostMapping("/delete")
    public ResponseResult delete(String ids) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",ids.split(","));
        attributeService.remove(updateWrapper);
        return new ResponseResult(200, "删除成功");
    }

    @ApiOperation("创建属性表")
    @PostMapping("/create")
    public ResponseResult create(@RequestBody ProductAttribute productAttribute) {
        attributeService.save(productAttribute);
        return new ResponseResult(200,"创建成功");
    }

    @ApiOperation("更改属性表")
    @PostMapping("/update/{id}")
    public ResponseResult update(@PathVariable("id") long id,
                                 @RequestBody ProductAttribute productAttribute) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        attributeService.update(productAttribute, updateWrapper);
        return new ResponseResult(200, "更改成功");
    }

    @ApiOperation("获取属性表")
    @GetMapping("/{id}")
    public ResponseResult get(@PathVariable long id) {
        ProductAttribute byId = attributeService.getById(id);
        return new ResponseResult(200,"查询成功", byId);
    }

    @ApiOperation("获取属性信息")
    @GetMapping("/attrInfo/{productCategoryId}")
    public ResponseResult atttInfo(@PathVariable("productCategoryId") long productCategoryId) {
        ProductCategory productCategory = attributeService.selectProductCategory(productCategoryId);
        return new ResponseResult(200, "查询成功", productCategory);
    }

}
